package com.conan.zone.system.web.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;

import com.conan.zone.system.web.action.LoginAction;

/**
 * 检测是否登录
 * Servlet Filter implementation class LoginCheckFilter
 */
public class LoginCheckFilter implements Filter {

    /**
     * Default constructor. 
     */
    public LoginCheckFilter() {
    	super();
    }

	/**
	 * 
	 */
	public void destroy() { }

	/**
	 * 
	 */
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		HttpServletRequest req = (HttpServletRequest)request;
		HttpServletResponse res = (HttpServletResponse)response;
		//System.out.println(req.getRequestURI());
		if(StringUtils.endsWith(req.getRequestURI(), "toLogin.html")
				|| StringUtils.endsWith(req.getRequestURI(), "login.html")){
			chain.doFilter(request, response);
		}else{
			if(LoginAction.getUserSession(req)==null){
				res.sendRedirect(req.getContextPath()+"/toLogin.html");
			}else{
				chain.doFilter(request, response);
			}
		}
	}

	/**
	 * @see Filter#init(FilterConfig)
	 */
	public void init(FilterConfig fConfig) throws ServletException { }

}
